package my.learnDB.tools;

import my.learnDB.dataSource.C3P0Util;
import my.learnDB.models.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.JdbcUtils;

import java.sql.SQLException;
import java.util.List;


/**
 * 使用：jdbcTemplate.update(insert, update, delete), jdbcTemplate.query( select  ), jdbcTemplate.queryForObject( 聚合 )这三类
 *  delete from account where id = 1
 *
 * 不要使用 jdbcTemplate.queryForList()
 */
public class ACJdbcTemplate {

    public static void main(String[] args) throws SQLException {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(C3P0Util.getDataSource());

        int update = jdbcTemplate.update("insert into account (name, money) value (?, ?)", "小菜", 2000f);
        System.out.println(update);

        List<Account> query = jdbcTemplate.query("select * from account where money1 > ?", new BeanPropertyRowMapper<>(Account.class), 100);
        System.out.println(query);

        // 聚合函数
        Integer integer = jdbcTemplate.queryForObject("select count(*) from account where money > ?", int.class, 100);
        System.out.println(integer);

        // List<Map<String,Object>>
        // List<Account> accounts = jdbcTemplate.queryForList("select * from account where id > ?", Account.class, 1);
        // System.out.println(accounts);

    }

}
